'use server'

import { b } from '../index';
import type { Check, Checked  } from "../types";
import type { Image, Audio, Pdf, Video } from "@boundaryml/baml";

import type { {% for t in types %} {{ t }}{% if !loop.last %}, {% endif %}{% endfor %} } from "../types"

import type * as types from "../types"

/**
 * Streaming BAML server actions that return ReadableStreams.
 */
{%- for func in functions %}

/**
 * Executes the streaming variant of the "{{ func.name }}" BAML action.
 *
 * This action initiates a streaming response by calling the corresponding
 * BAML stream function. The returned stream yields incremental updates.
 *
{%- for (name, arg_type) in func.args %}
 * @param { {{arg_type.serialize_type(pkg)}} } {{ name }}{% if arg_type.meta().is_optional() %} (optional){% endif %} - Input parameter.
{%- endfor %}
 *
 * @returns {ReadableStream<Uint8Array>} A stream that yields incremental updates from the action.
 */
export const {{ func.name }} = async (
  {%- for (name, arg_type) in func.args %}
  {{ name }}{% if arg_type.meta().is_optional() %}?{% endif %}: {{arg_type.serialize_type(pkg)}},
  {%- endfor %}
): Promise<ReadableStream<Uint8Array>> => {
  const stream = b.stream.{{ func.name }}(
    {%- for (name, _) in func.args %}
    {{ name }},
    {%- endfor %}
  );
  return Promise.resolve(stream.toStreamable());
};
{%- endfor %}